﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;
using System.Collections.Generic;

namespace Microsoft.ProductivityHub.SilverlightBookshelf
{
    
	public partial class FeaturedVideoControl : UserControl
	{
        private bool _isPlaying = false;
        SharePointData sd = new SharePointData();
        public event Bookshelf.FavoritesUpdated OnFavoritesUpdated;

        public FeaturedVideoControl()
		{
			// Required to initialize variables
			InitializeComponent();
            
            sd.DataLoadedEvent += new SharePointData.DataLoaded(sd_DataLoadedEvent);
            sd.QueryFeaturedVideo();

		}

        void sd_DataLoadedEvent(List<ContentItem> contentItems)
        {
            videoContentControl.ItemsSource = contentItems;

            if (contentItems !=null && contentItems.Count > 0)
            {
                MyPlayer.AutoPlay = false;
                MyPlayer.Source = new Uri(contentItems[0].DocUrl);
                MyPlayer.MouseLeftButtonDown += new MouseButtonEventHandler(MyPlayer_MouseLeftButtonDown);
            }


        }

        public void PauseVideo()
        {
            if (_isPlaying)
            {
                MyPlayer.Pause();
                MyPlayerTooltip.Text = "Click to play video";
                _isPlaying = false;
            }
        }

        void MyPlayer_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (_isPlaying)
            {
                MyPlayer.Pause();
                MyPlayerTooltip.Text = "Click to play video";
                _isPlaying = false;
            }
            else
            {
                MyPlayer.Play();
                MyPlayerTooltip.Text = "Click to pause video";
                _isPlaying = true;
            }
        }

        private void Product_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            // product text 
            string currentSiteUrl = Microsoft.SharePoint.Client.ApplicationContext.Current.Url;
            string product = ((TextBlock)sender).Text;
            if (!String.IsNullOrEmpty(currentSiteUrl))
            {
                string url = currentSiteUrl + String.Format("/Pages/Product.aspx?Product={0}", product);
                HtmlPage.Window.Navigate(new Uri(url));
            }
            
        }


        private void Explore_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            string currentSiteUrl = Microsoft.SharePoint.Client.ApplicationContext.Current.Url;
            if (!String.IsNullOrEmpty(currentSiteUrl))
            {
                string url = currentSiteUrl + "/Pages/Videos.aspx";
                HtmlPage.Window.Navigate(new Uri(url));
            }
        }

        private void TagFavorite_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            string docId = ((TextBlock)sender).Tag as string;
            string currentSiteUrl = Microsoft.SharePoint.Client.ApplicationContext.Current.Url;
            if (!String.IsNullOrEmpty(currentSiteUrl))
            {
                // we have sharepoint context, attempt the script call
                // javascript code to call: javascript:hub10_AddFavorite({@ID}) 
                string scriptCall = String.Format("hub10_AddFavorite({0});", docId);
                HtmlPage.Window.Eval(scriptCall);

                this.Dispatcher.BeginInvoke(OnFavoritesUpdated, this);
            }
        }

	}
}